package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.vo.UserVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppGoodsCartBo;
import com.ruoyi.app.domain.vo.AppGoodsCartVo;
import com.ruoyi.app.domain.AppGoodsCart;
import com.ruoyi.app.mapper.AppGoodsCartMapper;
import com.ruoyi.app.service.IAppGoodsCartService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP商城购物车Service业务层处理
 *
 * @author heike
 * @date 2023-03-02
 */
@RequiredArgsConstructor
@Service
public class AppGoodsCartServiceImpl implements IAppGoodsCartService {

    private final AppGoodsCartMapper baseMapper;

    /**
     * 查询APP商城购物车
     */
    @Override
    public AppGoodsCartVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP商城购物车列表
     */
    @Override
    public TableDataInfo<AppGoodsCartVo> queryPageList(AppGoodsCartBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppGoodsCart> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(AppGoodsCart::getCreateTime);
        Page<AppGoodsCartVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP商城购物车列表
     */
    @Override
    public List<AppGoodsCartVo> queryList(AppGoodsCartBo bo) {
        LambdaQueryWrapper<AppGoodsCart> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public AppGoodsCartVo queryOne(Long userId, Long goodId) {
        LambdaQueryWrapper<AppGoodsCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppGoodsCart::getUserId, userId).eq(AppGoodsCart::getGoodsId, goodId);
        return baseMapper.selectVoOne(wrapper);
    }

    private LambdaQueryWrapper<AppGoodsCart> buildQueryWrapper(AppGoodsCartBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppGoodsCart> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsId() != null, AppGoodsCart::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getUserId() != null, AppGoodsCart::getUserId, bo.getUserId());
        lqw.eq(bo.getNum() != null, AppGoodsCart::getNum, bo.getNum());
        lqw.orderByDesc(AppGoodsCart::getCreateTime);
        return lqw;
    }

    /**
     * 新增APP商城购物车
     */
    @Override
    public Boolean insertByBo(AppGoodsCartBo bo) {
        AppGoodsCart add = BeanUtil.toBean(bo, AppGoodsCart.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP商城购物车
     */
    @Override
    public Boolean updateByBo(AppGoodsCartBo bo) {
        AppGoodsCart update = BeanUtil.toBean(bo, AppGoodsCart.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppGoodsCart entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP商城购物车
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean updateNumber(Long id, String type) {
        AppGoodsCartVo appGoodsCartVo = queryById(id);
        if (type.equals(Constants.SUCCESS)) {
            int num = appGoodsCartVo.getNum() - 1;
            appGoodsCartVo.setNum(num <= 0 ? 1 : num);
        } else {
            appGoodsCartVo.setNum(appGoodsCartVo.getNum() + 1);
        }
        AppGoodsCartBo appGoodsCartBo = new AppGoodsCartBo();
        appGoodsCartBo.setId(id);
        appGoodsCartBo.setNum(appGoodsCartVo.getNum());
        return updateByBo(appGoodsCartBo);
    }
}
